#! /usr/bin/python
# encoding: utf-8

from fabric.api import *
import flask
import subprocess
import time

app = flask.Flask(__name__)

@app.route("/wechat")
def show_wechat_mcs_log():
#    return "Hello world"
#    return run('tailf /data/logs/demo2do-mobile-showcase/demo2do-mobile-showcase.log') 
    def inner():
        p1 = subprocess.Popen(
            ['fab -f fabfile.py wechat'],             #call something with a lot of output so we can see it
            shell=True,
            stdout=subprocess.PIPE
        )

        for line in iter(p1.stdout.readline,''):
#            time.sleep(1)                           # Don't need this just shows the text streaming
            yield line.rstrip() + '<br/>\n'

    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show th$

@app.route("/mobile")
def show_mobile_showcase_log():
    def inner():
        p2 = subprocess.Popen(
            ['fab -f fabfile.py mobile'],             #call something with a lot of output so we can see it
            shell=True,
            stdout=subprocess.PIPE
        )

        for line in iter(p2.stdout.readline,''):
            yield line.rstrip() + '<br/>\n'

    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show th$


@app.route("/payment")
def show_web_payment_log():
    def inner():
        p3 = subprocess.Popen(
            ['fab -f fabfile.py payment'],             #call something with a lot of output so we can see it
            shell=True,
            stdout=subprocess.PIPE
        )

        for line in iter(p3.stdout.readline,''):
            yield line.rstrip() + '<br/>\n'

    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show th$

#app.run()
if __name__ == "__main__":
    app.run( debug=True)
